/*
 * @Description:
 * @Version: 1.0.0
 * @Author: gyh
 * @Date: 2021-08-17 22:28:59
 * @LastEditors: gyh
 * @LastEditTime: 2021-08-17 23:09:40
 */
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'
const path = require('path')

// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [
      vue(),
      styleImport({
        libs: [
          {
            libraryName: 'element-plus',
            esModule: true,
            ensureStyleFile: true,
            resolveStyle: name => {
              return `element-plus/packages/theme-chalk/src/${name}.scss`
            },
            resolveComponent: name => {
              return `element-plus/lib/${name}`
            },
          },
        ],
      }),
    ],
    base: `/${loadEnv(mode, process.cwd()).VITE_APP_NAME}/`,
    server: {
      host: 'localhost',
      port: 8080,
      open: true, // 是否自动在浏览器打开
      // https: false, // 是否开启 https
      // ssr: false, // 服务端渲染
      proxy: {
        '/api': {
          target: loadEnv(mode, process.cwd()).VITE_APP_BASE_GATEWAY,
          changeOrigin: true,
          ws: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: path.resolve(__dirname, 'src'),
        },
      ],
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/assets/styles/variables.scss";`,
        },
      },
    },
  })
}
